package com.xywh.emergency_outbound.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.CheckBox;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xw.repo.XEditText;
import com.xywh.emergency_outbound.R;
import com.xywh.emergency_outbound.di.component.DaggerAddressEditComponent;
import com.xywh.emergency_outbound.di.module.AddressEditModule;
import com.xywh.emergency_outbound.mvp.contract.AddressEditContract;
import com.xywh.emergency_outbound.mvp.presenter.AddressEditPresenter;
import com.xywh.emergency_outbound.mvp.ui.entity.AddressEdit;
import com.xywh.emergency_outbound.mvp.ui.entity.Tab4;

import org.jetbrains.annotations.NotNull;
import org.simple.eventbus.EventBus;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.commonres.utils.CProgressDialogUtils;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import okhttp3.RequestBody;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.ACTIVITY_ADDRESS_EDIT)
public class AddressEditActivity extends BaseActivity<AddressEditPresenter> implements AddressEditContract.View {

    @BindView(R.id.xet_receiver)
    XEditText xetReceiver;
    @BindView(R.id.xet_cellphone_number)
    XEditText xetCellphoneNumber;
    @BindView(R.id.xet_area)
    XEditText xetArea;
    @BindView(R.id.xet_address)
    XEditText xetAddress;
    @BindView(R.id.cb_default)
    CheckBox cbDefault;
    @BindView(R.id.btn_save)
    Button btnSave;

    private Tab4.DataBean mDataBean;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerAddressEditComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .addressEditModule(new AddressEditModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_address_edit; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_address_edit));
//        getAddressEditData();
        try {
            mDataBean = (Tab4.DataBean) getIntent().getExtras().getSerializable("address_item");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (mDataBean != null) {
            xetReceiver.setText(mDataBean.getConsigneeName());
            xetCellphoneNumber.setText(mDataBean.getLinkPhone());
            xetAddress.setText(mDataBean.getEmgConsigneeAddresses().get(0).getAddress());

            if (mDataBean.isIsDefault()) {
                cbDefault.setChecked(true);
            }
        }


    }

    private void getAddressEditData() {
        HashMap<String, String> map = new HashMap<>();

//        mPresenter.getAddressEditData(map);
    }

    @Override
    public void getAddressEditDataSuccess(AddressEdit entityData) {
        showMessage(entityData.getMessage());
        if (entityData.getCode() == 200) {
            EventBus.getDefault().post("0", BaseConstants.ADDRESS_LIST);
            finish();
        }

    }


    @Override
    public void showLoading() {
        CProgressDialogUtils.showProgressDialog(this);
    }

    @Override
    public void hideLoading() {
        CProgressDialogUtils.cancelProgressDialog();
    }


    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @OnClick(R.id.btn_save)
    public void onViewClicked() {
        if (TextUtils.isEmpty(getReceiver())) {
            showMessage("请输入收货人");
            return;
        }
        if (TextUtils.isEmpty(getCellphoneNumber())) {
            showMessage("请输入手机号码");
            return;
        }
//        if (TextUtils.isEmpty(getArea())) {
//            showMessage("请输入所在地区");
//            return;
//        }
        if (TextUtils.isEmpty(getAddress())) {
            showMessage("请输入详细地址");
            return;
        }
        String isDefault = "false";
        if (cbDefault.isChecked()) {
            isDefault = "true";
        }

        String jsonData = "{\"EmgConsigneeAddresses\": [\n" +
                "            {\n" +
                "                \"EmgConsignee\": null,\n" +
                "                \"ID\": " + mDataBean.getEmgConsigneeAddresses().get(0).getID() + ",\n" +
                "                \"EmgConsigneeId\": " + mDataBean.getID() + ",\n" +
                "                \"Address\": \"" + getArea() + getAddress() + "\",\n" +
                "                \"IsDefault\": false,\n" +
                "                \"IsMixSpot\": false,\n" +
                "                \"InsertUserId\": null,\n" +
                "                \"InsertDateTime\": null,\n" +
                "                \"ModifyUserId\": null,\n" +
                "                \"ModifyDateTime\": null\n" +
                "            }\n" +
                "        ],\n" +
                "        \"ID\": " + mDataBean.getID() + ",\n" +
                "        \"ConsigneeName\": \"" + getReceiver() + "\",\n" +
                "        \"LinkPhone\": \"" + getCellphoneNumber() + "\",\n" +
                "        \"IsDefault\": " + isDefault + ",\n" +
                "        \"InsertUserId\": " + mSharedPreferencesHelper.getString(BaseConstants.UID) + ",\n" +
                "        \"InsertDateTime\": null,\n" +
                "        \"ModifyUserId\": null,\n" +
                "        \"ModifyDateTime\": null\n" +
                "    }";
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonData);
        mPresenter.getAddressEditData(body);
    }

    @NotNull
    private String getReceiver() {
        return xetReceiver.getText().toString().trim();
    }

    @NotNull
    private String getCellphoneNumber() {
        return xetCellphoneNumber.getText().toString().trim();
    }

    @NotNull
    private String getAddress() {
        return xetAddress.getText().toString().trim();
    }

    @NotNull
    private String getArea() {
        return xetArea.getText().toString().trim();
    }
}
